#!/usr/bin/python2.7
import ctypes as ct
import socket
import time

def struct2stream(s):
	length 	= ct.sizeof(s)
	p		= ct.cast(ct.pointer(s), ct.POINTER(ct.c_char*length))
	return p.contents.raw

def stream2struct(string, stype):
	if not issubclass(stype, ct.Structure):
		raise ValueError('The type of the struct is not a ctypes.Structure')

	length		= ct.sizeof(stype)
	stream		= (ct.c_char * length)()
	stream.raw	= string
	p			= ct.cast(stream, ct.POINTER(stype))
	return p.contents

class StructPointerTest(ct.LittleEndianStructure):
	_pack_		= 1
	_fields_ 	=[\
			('version', ct.c_uint32),\
			('length', ct.c_uint32),\
			('command_id', ct.c_uint32),\
			('sequence', ct.c_uint32),\
			('status', ct.c_uint32)
	]

if __name__ == "__main__" :
	print "hellow"
	xx = StructPointerTest

	xx.version = 0x00020001
	xx.length = 0
	xx.command_id = 0x00006005
	xx.sequence = 0
	xx.status = 0

	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock.connect(('127.0.0.1',9999))

	while True:
		buf=struct2stream(xx)

		sock.send(buf)
		time.sleep(3)

	sock.close()

